package com.zty.xiaomiadmin.server.Service.Report;

import com.zty.xiaomiadmin.server.Dao.Login;
import com.zty.xiaomiadmin.server.Dao.ReportMapper;
import com.zty.xiaomiadmin.server.Dao.Suggest;
import com.zty.xiaomiadmin.server.Entity.Report.Report;
import com.zty.xiaomiadmin.server.Entity.Report.ReportPie;
import com.zty.xiaomiadmin.server.Entity.Report.ReportPieResult;
import com.zty.xiaomiadmin.server.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;


@Service
public class ReportServiceImpl implements ReportService {
    @Override
    public List<Report> getMonthData() throws IOException {
        try {
            List<Report> reportList = SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(ReportMapper.class).getMonthData();
            return reportList;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Report> getReportDataByMonth(String date) {
        List<Report> reportList = null;
        try {
            reportList = SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(ReportMapper.class).getReportDataByMonth(date);
            List<String> days = getMonthFullDay(date);
            for (String day : days) {
                boolean flag = false;
                for (int i = 0; i < reportList.size(); i++) {
                    if (reportList.get(i).getCreateTime().equals(day)) {
                        flag = true;
                        break;
                    }
                }
                if (!flag) {
                    Report report = new Report();
                    report.setCreateTime(day);
                    report.setPayment(0);
                    reportList.add(report);
                }
            }
            reportList.sort((o1, o2) -> o1.getCreateTime().compareTo(o2.getCreateTime()) > 0 ? 1 : -1);
            return reportList;
        }catch (Exception e){
            e.printStackTrace();
        }
        return reportList;
    }

    @Override
    public List<Report> getReportDataByMonthPay(String date) {
        List<Report> reportList = null;
        try {
            reportList = SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(ReportMapper.class).getReportDataByMonthPay(date);
            List<String> days = getMonthFullDay(date);
            for (String day : days) {
                boolean flag = false;
                for (int i = 0; i < reportList.size(); i++) {
                    if (reportList.get(i).getCreateTime().equals(day)) {
                        flag = true;
                        break;
                    }
                }
                if (!flag) {
                    Report report = new Report();
                    report.setCreateTime(day);
                    report.setPayment(0);
                    reportList.add(report);
                }
            }
            reportList.sort((o1, o2) -> o1.getCreateTime().compareTo(o2.getCreateTime()) > 0 ? 1 : -1);
            return reportList;
        }catch (Exception e){
            e.printStackTrace();
        }
        return reportList;
    }

    @Override
    public List<Report> getReportDataByMonthCancel(String date) {
        List<Report> reportList = null;
        try {
            reportList = SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(ReportMapper.class).getReportDataByMonthCancel(date);
            List<String> days = getMonthFullDay(date);
            for (String day : days) {
                boolean flag = false;
                for (int i = 0; i < reportList.size(); i++) {
                    if (reportList.get(i).getCreateTime().equals(day)) {
                        flag = true;
                        break;
                    }
                }
                if (!flag) {
                    Report report = new Report();
                    report.setCreateTime(day);
                    report.setPayment(0);
                    reportList.add(report);
                }
            }
            reportList.sort((o1, o2) -> o1.getCreateTime().compareTo(o2.getCreateTime()) > 0 ? 1 : -1);
            return reportList;
        }catch (Exception e){
            e.printStackTrace();
        }
        return reportList;
    }

    @Override
    public List<ReportPie> getReportDataByMonthPIE(String date) {
        List<ReportPie> reportPieList = new ArrayList<>();
        try {
            ReportPie accessR = SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(ReportMapper.class).getReportDataByMonthPaymentTotalAccess(date);
            ReportPie payR = SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(ReportMapper.class).getReportDataByMonthPaymentTotalPay(date);
            ReportPie cancelR = SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(ReportMapper.class).getReportDataByMonthPaymentTotalCancel(date);
            if (accessR != null) {
                accessR.setName("交易成功");
                reportPieList.add(accessR);
            }else {
                ReportPie reportPie = new ReportPie();
                reportPie.setName("交易成功");
                reportPie.setValue(0);
                reportPieList.add(reportPie);
            }
            if (payR != null) {
                payR.setName("待完成");
                reportPieList.add(payR);
            }else {
                ReportPie reportPie = new ReportPie();
                reportPie.setName("待完成");
                reportPie.setValue(0);
                reportPieList.add(reportPie);
            }
            if (cancelR != null) {
                cancelR.setName("交易取消");
                reportPieList.add(cancelR);
            }else {
                ReportPie reportPie = new ReportPie();
                reportPie.setName("交易取消");
                reportPie.setValue(0);
                reportPieList.add(reportPie);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return reportPieList;
    }

    //季度
    @Override
    public List<ReportPie> getReportDataByQuarter(List<String> list) {
        List<ReportPie> reportList = new ArrayList<>();
        try {
            for (String s : list) {
                ReportPie accessR = SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(ReportMapper.class).getReportDataByMonthPaymentTotalAccess(s);
                if (accessR != null) {
                    accessR.setName(s);
                    reportList.add(accessR);
                }else {
                    ReportPie reportPie = new ReportPie();
                    reportPie.setName(s);
                    reportPie.setValue(0);
                    reportList.add(reportPie);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return reportList;
    }

    @Override
    public List<ReportPie> getReportDataByQuarterPay(List<String> list) {
        List<ReportPie> reportList = new ArrayList<>();
        try {
            for (String s : list) {
                ReportPie accessR = SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(ReportMapper.class).getReportDataByMonthPaymentTotalPay(s);
                if (accessR != null) {
                    accessR.setName(s);
                    reportList.add(accessR);
                }else {
                    ReportPie reportPie = new ReportPie();
                    reportPie.setName(s);
                    reportPie.setValue(0);
                    reportList.add(reportPie);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return reportList;
    }

    @Override
    public List<ReportPie> getReportDataByQuarterCancel(List<String> list) {
        List<ReportPie> reportList = new ArrayList<>();
        try {
            for (String s : list) {
                ReportPie accessR = SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(ReportMapper.class).getReportDataByMonthPaymentTotalCancel(s);
                if (accessR != null) {
                    accessR.setName(s);
                    reportList.add(accessR);
                }else {
                    ReportPie reportPie = new ReportPie();
                    reportPie.setName(s);
                    reportPie.setValue(0);
                    reportList.add(reportPie);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return reportList;
    }

    @Override
    public List<ReportPie> getReportDataByQuarterPIE(List<String> list) {
        List<ReportPie> reportPieList = new ArrayList<>();
        try {
            int accessRTotal = 0;
            int payRTotal = 0;
            int cancelRTotal = 0;
            for (String s : list) {
                ReportPie accessR = SqlSessionUtil.getNewSqlSessionInstance().getSqlSession().getMapper(ReportMapper.class).getReportDataByMonthPaymentTotalAccess(s);
                ReportPie payR = SqlSessionUtil.getNewSqlSessionInstance().getSqlSession().getMapper(ReportMapper.class).getReportDataByMonthPaymentTotalPay(s);
                ReportPie cancelR = SqlSessionUtil.getNewSqlSessionInstance().getSqlSession().getMapper(ReportMapper.class).getReportDataByMonthPaymentTotalCancel(s);
                if (accessR == null){
                    accessR = new ReportPie();
                    accessR.setValue(0);
                }
                if (payR == null){
                    payR = new ReportPie();
                    payR.setValue(0);
                }
                if (cancelR == null){
                    cancelR = new ReportPie();
                    cancelR.setValue(0);
                }
                accessRTotal+=accessR.getValue();
                payRTotal+=payR.getValue();
                cancelRTotal+=cancelR.getValue();
            }
            ReportPie acr=new ReportPie();
            acr.setName("交易成功");
            acr.setValue(accessRTotal);
            reportPieList.add(acr);
            ReportPie nop=new ReportPie();
            nop.setName("待完成");
            nop.setValue(payRTotal);
            reportPieList.add(nop);
            ReportPie canc=new ReportPie();
            canc.setName("交易取消");
            canc.setValue(cancelRTotal);
            reportPieList.add(canc);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return reportPieList;
    }

    @Override
    public List<Report> getReportDataByYear(String date) {
        List<String> monthList = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            if (i < 9) {
                monthList.add(date + "-0" + (i + 1));
            }else {
                monthList.add(date + "-" + (i + 1));
            }
        }
        List<Report> reportList = null;
        try {
            reportList = SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(ReportMapper.class).getReportDataByYear(date);
            System.out.println(reportList);
            for (String month : monthList) {
                boolean flag = false;
                for (int i = 0; i < reportList.size(); i++) {
                    if (reportList.get(i).getCreateTime().equals(month)) {
                        flag = true;
                        break;
                    }
                }
                if (!flag) {
                    Report report = new Report();
                    report.setCreateTime(month);
                    report.setPayment(0);
                    reportList.add(report);
                }
            }
            reportList.sort((o1, o2) -> o1.getCreateTime().compareTo(o2.getCreateTime()) > 0 ? 1 : -1);
            return reportList;
        }catch (Exception e){
            e.printStackTrace();
        }
        return reportList;
    }

    @Override
    public List<Report> getReportDataByYearPay(String date) {
        List<String> monthList = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            if (i < 9) {
                monthList.add(date + "-0" + (i + 1));
            }else {
                monthList.add(date + "-" + (i + 1));
            }
        }
        List<Report> reportList = null;
        try {
            reportList = SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(ReportMapper.class).getReportDataByYearPay(date);
            for (String month : monthList) {
                boolean flag = false;
                for (int i = 0; i < reportList.size(); i++) {
                    if (reportList.get(i).getCreateTime().equals(month)) {
                        flag = true;
                        break;
                    }
                }
                if (!flag) {
                    Report report = new Report();
                    report.setCreateTime(month);
                    report.setPayment(0);
                    reportList.add(report);
                }
            }
            reportList.sort((o1, o2) -> o1.getCreateTime().compareTo(o2.getCreateTime()) > 0 ? 1 : -1);
            return reportList;
        }catch (Exception e){
            e.printStackTrace();
        }
        return reportList;
    }

    @Override
    public List<Report> getReportDataByYearCancel(String date) {
        List<String> monthList = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            if (i < 9) {
                monthList.add(date + "-0" + (i + 1));
            }else {
                monthList.add(date + "-" + (i + 1));
            }
        }
        List<Report> reportList = null;
        try {
            reportList = SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(ReportMapper.class).getReportDataByYearCancel(date);
            for (String month : monthList) {
                boolean flag = false;
                for (int i = 0; i < reportList.size(); i++) {
                    if (reportList.get(i).getCreateTime().equals(month)) {
                        flag = true;
                        break;
                    }
                }
                if (!flag) {
                    Report report = new Report();
                    report.setCreateTime(month);
                    report.setPayment(0);
                    reportList.add(report);
                }
            }
            reportList.sort((o1, o2) -> o1.getCreateTime().compareTo(o2.getCreateTime()) > 0 ? 1 : -1);
            return reportList;
        }catch (Exception e){
            e.printStackTrace();
        }
        return reportList;
    }

    @Override
    public List<ReportPie> getReportDataByYearPIE(String date) {
        List<ReportPie> reportPieList = new ArrayList<>();
        try {
            ReportPie accessR = SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(ReportMapper.class).getReportDataByYearPaymentTotalAccess(date);
            ReportPie payR = SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(ReportMapper.class).getReportDataByYearPaymentTotalPay(date);
            ReportPie cancelR = SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(ReportMapper.class).getReportDataByYearPaymentTotalCancel(date);
            if (accessR != null) {
                accessR.setName("交易成功");
                reportPieList.add(accessR);
            }else {
                ReportPie reportPie = new ReportPie();
                reportPie.setName("交易成功");
                reportPie.setValue(0);
                reportPieList.add(reportPie);
            }
            if (payR != null) {
                payR.setName("待完成");
                reportPieList.add(payR);
            }else {
                ReportPie reportPie = new ReportPie();
                reportPie.setName("待完成");
                reportPie.setValue(0);
                reportPieList.add(reportPie);
            }
            if (cancelR != null) {
                cancelR.setName("交易取消");
                reportPieList.add(cancelR);
            }else {
                ReportPie reportPie = new ReportPie();
                reportPie.setName("交易取消");
                reportPie.setValue(0);
                reportPieList.add(reportPie);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return reportPieList;
    }

    public static List<String> getMonthFullDay(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<String> fullDayList = new ArrayList<String>();
        int year = Integer.parseInt(date.substring(0, 4));
        int month = Integer.parseInt(date.substring(5, 7));
        int day = 1;// 所有月份从1号开始
        Calendar cal = Calendar.getInstance();// 获得当前日期对象
        cal.clear();// 清除信息
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);// 1月从0开始
        cal.set(Calendar.DAY_OF_MONTH, day);
        int count = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        for (int j = 0; j <= (count - 1); ) {
            if (sdf.format(cal.getTime()).equals(getLastDay(year, month))) {
                break;
            }
            cal.add(Calendar.DAY_OF_MONTH, j == 0 ? +0 : +1);
            j++;
            fullDayList.add(sdf.format(cal.getTime()));
        }
        return fullDayList;
    }
    public static String getLastDay(int year, int month) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DAY_OF_MONTH, 0);
        return sdf.format(cal.getTime());
    }
}
